/********************************************************
 *
 * FILE NAME:   splitter.h
 *
 * DESCRIPTION:
 *  Module with signal splitter component
 *
 ********************************************************/

#ifndef SPLITTER_H
#define SPLITTER_H

/********************************************************/
/*              Includes                                */
/********************************************************/
#include "buffer.h"
#include "signalprocessing.h"

#include <QObject>
#include <QThread>
#include <QMutex>
#include <QWaitCondition>

/********************************************************/
/*              Defines                                 */
/********************************************************/

/********************************************************/
/*              Macros                                  */
/********************************************************/

/********************************************************/
/*              Typedefs                                */
/********************************************************/
class Splitter;

class Splitter : public QObject
{
    Q_OBJECT

    /********************************************************/
    /*              Typedefs                                */
    /********************************************************/
    public:

    protected:

    private:
        class MasterOSignal;
        class SlaveOSignal;

        class MasterOSignal : public OSignal
        {
            /********************************************************/
            /*              Typedefs                                */
            /********************************************************/
            public:

            protected:

            private:

            /********************************************************/
            /*              Functions Declarations                  */
            /********************************************************/
            public:
                /********************************************************
                 * Function Name    :   MasterOSignal
                 *
                 * Description      :   Constructor
                 *
                 * Side effects     :
                 *
                 * Comment          :
                 *
                 * Parameters       :
                 *
                 * Returns      	:
                 *
                 * Reviewed by      :   v_duschan@hotmail.com
                 *
                 ********************************************************/
                MasterOSignal(Splitter &context);

                /********************************************************
                 * Function Name    :   onBurstRequest
                 *
                 * Description      :   Called when new burst of samples requested
                 *
                 * Side effects     :
                 *
                 * Comment          :
                 *
                 * Parameters       :   size_t burstLength = burst length
                 *
                 * Returns      	:   true if burst of samples successfully processed, else false
                 *
                 * Reviewed by      :   v_duschan@hotmail.com
                 *
                 ********************************************************/
                bool onBurstRequest(size_t burstLength);

            protected:

            private:

            /********************************************************/
            /*              Variables Declarations                  */
            /********************************************************/
            public:

            protected:

            private:
                Splitter &_context;
        };
        class SlaveOSignal : public OSignal
        {
            /********************************************************/
            /*              Typedefs                                */
            /********************************************************/
            public:

            protected:

            private:

            /********************************************************/
            /*              Functions Declarations                  */
            /********************************************************/
            public:
                /********************************************************
                 * Function Name    :   MasterOSignal
                 *
                 * Description      :   Constructor
                 *
                 * Side effects     :
                 *
                 * Comment          :
                 *
                 * Parameters       :
                 *
                 * Returns      	:
                 *
                 * Reviewed by      :   v_duschan@hotmail.com
                 *
                 ********************************************************/
                SlaveOSignal(Splitter &context);

                /********************************************************
                 * Function Name    :   onBurstRequest
                 *
                 * Description      :   Called when new burst of samples requested
                 *
                 * Side effects     :
                 *
                 * Comment          :
                 *
                 * Parameters       :   size_t burstLength = burst length
                 *
                 * Returns      	:   true if burst of samples successfully processed, else false
                 *
                 * Reviewed by      :   v_duschan@hotmail.com
                 *
                 ********************************************************/
                bool onBurstRequest(size_t burstLength);

            protected:

            private:

            /********************************************************/
            /*              Variables Declarations                  */
            /********************************************************/
            public:

            protected:

            private:
                Splitter &_context;
        };

    /********************************************************/
    /*              Functions Declarations                  */
    /********************************************************/
    public:
        /********************************************************
         * Function Name    :   Splitter
         *
         * Description      :   Constructor
         *
         * Side effects     :
         *
         * Comment          :
         *
         * Parameters       :   QObject *parent = Qt Object parent of the Splitter object
         *
         * Returns      	:
         *
         * Reviewed by      :   v_duschan@hotmail.com
         *
         ********************************************************/
        Splitter(QObject *parent = 0);

        /********************************************************
         * Function Name    :   registerTypes
         *
         * Description      :   Register Splitter class to QML
         *
         * Side effects     :
         *
         * Comment          :
         *
         * Parameters       :
         *
         * Returns      	:
         *
         * Reviewed by      :   v_duschan@hotmail.com
         *
         ********************************************************/
        static void registerTypes(const char *uri);

        Q_INVOKABLE bool init(int burstLength);

        Q_INVOKABLE EXPORT_SIGNAL(_inISig, ISignal)
        Q_INVOKABLE EXPORT_SIGNAL(_masterOSig, MasterOSignal)
        Q_INVOKABLE EXPORT_SIGNAL(_slaveOSig, SlaveOSignal)

    protected:

    private:

    /********************************************************/
    /*              Variables Declarations                  */
    /********************************************************/
    public:

    protected:

    private:
        size_t _burstLength;
        ISignal *_inISig;

        MasterOSignal *_masterOSig;

        Buffer<sample_t> *_slaveBuff;
        size_t _slaveUnprocessed;
        QMutex *_slaveMtx;
        QWaitCondition *_slaveNotEmpty;
        QWaitCondition *_slaveNotFull;
        SlaveOSignal *_slaveOSig;
};

/********************************************************/
/*              Functions Declarations                  */
/********************************************************/

/********************************************************/
/*              Variables Declarations (IMPORT)         */
/********************************************************/

#endif /* SPLITTER_H */
